﻿using Infrastructure.Attribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model;
using ZR.Repository;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;
using Infrastructure;
using Infrastructure.Extensions;
using System.Diagnostics;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 抽检表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksSpotCheckService), ServiceLifetime = LifeTime.Transient)]
    public class CksSpotCheckService : BaseService<CksSpotCheck>, ICksSpotCheckService {

        public PagedInfo<CksSpotCheckDto> GetList(CksSpotCheckQueryDto parm) {

            var response = Queryable()
                .WhereIF(!(string.IsNullOrEmpty(parm.PrimaryCode)), (recDt) => recDt.PrimaryCode == parm.PrimaryCode)
                .WhereIF(!string.IsNullOrEmpty(parm.BatchCode), (recDt) => recDt.BatchCode == parm.BatchCode)
                .WhereIF(parm.BeginTime.HasValue && parm.EndTime.HasValue, (recDt) => recDt.CreatedTime >= parm.BeginTime.Value && recDt.CreatedTime <= parm.EndTime.Value)
                .Select((recDt) => new CksSpotCheckDto {
                    SpotCheckId = recDt.SpotCheckId,
                    BatchCode = recDt.BatchCode,
                    PrimaryCode = recDt.PrimaryCode,
                    Type = recDt.Type,
                    CreatedTime = recDt.CreatedTime,
                    Creator = recDt.Creator
                }).ToPage(parm);

            return response;
        }

        public int UpdateCksSpotCheck(CksSpotCheck parm) {
            return Context.Updateable(parm).UpdateColumns(it => new {
                it.BatchCode,
                it.PrimaryCode,
                it.Type
            }).ExecuteCommand();
        }

        public int DeleteCksSpotCheckByIds(long[] ids) {
            return Delete(ids);
        }

        public (string, object, object) ImportSpotCheckData(List<CksSpotCheck> checkDtls) {
            List<int> types = new List<int>() { 1, 2 };
            string batchCode = $"CK{DateTime.Now.ToString("yyyyMMddHHmmss")}";
            checkDtls.ForEach(x => {
                x.BatchCode = batchCode;
                x.CreatedTime = DateTime.Now;
                x.Creator = App.UserName;
            });
            var x = Context.Storageable(checkDtls)
                .SplitInsert(it => !it.Any())
                .SplitError(x => x.Item.PrimaryCode.IsEmpty(), "主产品码不能为空")
                .SplitError(x => !types.Contains(x.Item.Type), "产品类型不符合规范")
                .WhereColumns(it => it.SpotCheckId)//如果不是主键可以这样实现（多字段it=>new{it.x1,it.x2}）
                .ToStorage();
            var result = x.AsInsertable.ExecuteCommand();//插入可插入部分;

            string msg = string.Format(" 插入{0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4} 总共{5}",
                               x.InsertList.Count,
                               x.UpdateList.Count,
                               x.ErrorList.Count,
                               x.IgnoreList.Count,
                               x.DeleteList.Count,
                               x.TotalList.Count);
            //输出统计                      
            Debug.WriteLine(msg);

            //输出错误信息               
            foreach (var item in x.ErrorList) {
                Debug.WriteLine("PrimaryCode" + item.Item.PrimaryCode + " : " + item.StorageMessage);
            }

            return (msg, x.ErrorList, x.IgnoreList);
        }
    }
}
